#SingleInstance force
#NoEnv
#NoTrayIcon
SetBatchLines -1
ListLines Off

; env
SetWorkingDir, %A_ScriptDir%
AhkExe = lib\ahk.exe

; rebuild mykeys.ahk from plugins
fileMyKeys = __mykeys_tmp.ahk
GoSub, RebuildMyKeys

; start my keys
Run, %AhkExe% %fileMyKeys% %1%

ExitApp


RebuildMyKeys:
	; clear
	FileDelete, %fileMyKeys%
	
	; header
	FileAppend,
	( LTrim ` %
		; persistent singleton
		#SingleInstance force
		#Persistent
		
		; for performance
		#NoEnv
		SetBatchLines -1
		ListLines Off
		
		; title mode, input mode
		SetTitleMatchMode RegEx
		SendMode Input
		
		; common env var and working dir
		SetWorkingDir, %A_ScriptDir%
		AhkExe = lib\ahk.exe
		
		; read settings.ini
		SettingsFile = %1%
		if (!SettingsFile || !FileExist(SettingsFile)) {
			SettingsFile = settings.ini
		}
		Loop, read, %SettingsFile%
		{
			; get key/value pair
			StringSplit, part, A_LoopReadLine, =, %A_Space%%A_Tab%
			if (!part1 || !part2 || Asc(part1)<65 || Asc(part1)>90 && Asc(part1)<97 || Asc(part1)>122)
				continue
			
			%part1% = %part2%
		}
		
		Menu, Tray, NoStandard

		; init plugins
		
	), %fileMyKeys%

	; init plugins
	Loop, plugin_*.ahk
	{
		SplitPath, A_LoopFileName, , , , name
		FileAppend, GoSub %name%`n, %fileMyKeys%
	}
	
	; end of auto execution
	FileAppend,
	( LTrim ` %

		; tray icon and menu
		Menu, Tray, Add
		Menu, Tray, Add, &Restart`t( %RestartKey% ), RestartKey
		Menu, Tray, Add, &Desktop`t( %DesktopKey% ), DesktopKey
		Menu, Tray, Add, &Clone self..., MenuCloneSelf
		Menu, Tray, Add, &Settings..., MenuOpenSettings
		Menu, Tray, Add
		Menu, Tray, Add, &Help, MenuHelp
		Menu, Tray, Add, &About, MenuAbout
		Menu, Tray, Add, E&xit, MenuExit
		
		Menu, Tray, Default, &Help
		Menu, Tray, Tip, My Keys
		Menu, Tray, Icon, lib\tray.ico

		; two basic keys
		RegisterKey(DesktopKey, "DesktopKey")
		RegisterKey(RestartKey, "RestartKey")
	
		; notify start
		TrayTip, My Keys, %LauncherKey%`tto open laucnher`nDouble Ctrl+C`tto copy to launcher, 1, 1
		
		return
		
		RestartKey:
			Run, mykeys.exe %SettingsFile%
		return
		
		DesktopKey:
			GoSub, GuiClose
			Run lib\Show Desktop.scf
		return
		
		MenuCloneSelf:
			Critical
			CloneSelf()
		return
		
		MenuOpenSettings:
			Run, notepad.exe settings.ini
		return
		
		MenuHelp:
			Run, http://code.google.com/p/mykeys/
		return
		
		MenuAbout:
			MsgBox, 64, My Keys, My Keys v1.0`n`nby Li Yang`n(young4chong@gmail.com)`n`nhttp://code.google.com/p/mykeys/
		return
		
		MenuExit:
			ExitApp
		return
		
		CloneSelf() {
			Critical
			global CloneSelfFiles
			
			; select target dir
			DriveGet, usb, List, REMOVABLE
			if (usb)
				usb := SubStr(usb, 1, 1) ":\"
			FileSelectFolder, targetDir, *%usb%, 3, Copy My Keys to which folder?
			if (!targetDir)
				return
			
			; confirm target dir
			if (SubStr(targetDir, StrLen(targetDir), 1) = "\")
				targetDir = %targetDir%mykeys
			else
				targetDir = %targetDir%\mykeys
			msgbox, 1, My Keys, Copy My Keys to %targetDir%?
			IfMsgBox Cancel
				return
		
			; do copy
			SplashImage,, B1,, Copying to %targetDir% ...,, Arial
			Loop, Parse, CloneSelfFiles, |, %A_Space%%A_Tab%
			{
				Loop, %A_LoopField%, 0, 1
				{
					if (InStr(A_LoopFileFullPath, ".svn"))
						continue
					file = %targetDir%\%A_LoopFileFullPath%
					SplitPath, file, , dir
					FileCreateDir, %dir%
					FileCopy, %A_LoopFileFullPath%, %file%, 1
				}
			}
			SplashImage, Off
			msgbox, 0, My Keys, My Keys was copied to %targetDir%.
		}
		
		#include lib\util.ahk
		#include lib\util_menuicon.ahk
		
	), %fileMyKeys%
	
	; include plugins
	Loop, plugin_*.ahk
	{
		FileAppend, #include %A_LoopFileName%`n, %fileMyKeys%
	}

return